package com.youhome.service.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.youhome.service.response.*;
import com.youhome.utils.DateUtils;
import com.youhome.utils.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2019/04/16.
 */
public class ZillowJsonExtractUtils {

    public static ZhviResult extractZhviResultByJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        ZhviResult zhviResult = new ZhviResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (!StringUtils.isEmpty(jsonObject.get("Zhvi"))) {
                zhviResult.setZhvi(Double.valueOf(jsonObject.get("Zhvi").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("MoM"))) {
                zhviResult.setMom(Double.valueOf(jsonObject.get("MoM").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("QoQ"))) {
                zhviResult.setQoq(Double.valueOf(jsonObject.get("QoQ").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("YoY"))) {
                zhviResult.setYoy(Double.valueOf(jsonObject.get("YoY").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("5Year"))) {
                zhviResult.setY_5(Double.valueOf(jsonObject.get("5Year").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("10Year"))) {
                zhviResult.setY_10(Double.valueOf(jsonObject.get("10Year").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("PeakMonth"))) {
                zhviResult.setPeak_month(jsonObject.get("PeakMonth").toString());
            }
            if (!StringUtils.isEmpty(jsonObject.get("PeakQuarter"))) {
                zhviResult.setPeak_quarter(jsonObject.get("PeakQuarter").toString());
            }
            if (!StringUtils.isEmpty(jsonObject.get("PeakZHVI"))) {
                zhviResult.setPeak_zhvi(Double.valueOf(jsonObject.get("PeakZHVI").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("PctFallFromPeak"))) {
                zhviResult.setPct_fall_from_peak(Double.valueOf(jsonObject.get("PctFallFromPeak").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("LastTimeAtCurrZHVI"))) {
                zhviResult.setLast_time_at_curr_zhvi(jsonObject.get("LastTimeAtCurrZHVI").toString());
            }
        }
        return zhviResult;
    }

    public static BuyerSellerResult extractBuyerSellerResultByJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        BuyerSellerResult buyerSellerResult = new BuyerSellerResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (!StringUtils.isEmpty(jsonObject.get("PctPriceCut"))) {
                buyerSellerResult.setPct_price_cut(Double.valueOf(jsonObject.get("PctPriceCut").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("DaysOnMarket"))) {
                buyerSellerResult.setDays_on_market(Double.valueOf(jsonObject.get("DaysOnMarket").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("BuyerSellerIndex"))) {
                buyerSellerResult.setBuyer_seller_index(Double.valueOf(jsonObject.get("BuyerSellerIndex").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("BuyerSellerIndexMetro"))) {
                buyerSellerResult.setBuyer_seller_index_metro(Double.valueOf(jsonObject.get("BuyerSellerIndexMetro").toString()));
            }
        }
        return buyerSellerResult;
    }

    public static MarketHealthResult extractMarketHealthResultByJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        MarketHealthResult marketHealthResult = new MarketHealthResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);

            if (!StringUtils.isEmpty(jsonObject.get("MarketHealthIndex"))) {
                marketHealthResult.setMarket_health_index(Double.valueOf(jsonObject.get("MarketHealthIndex").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("SellForGain"))) {
                marketHealthResult.setSell_for_gain(Double.valueOf(jsonObject.get("SellForGain").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("PrevForeclosed"))) {
                marketHealthResult.setPrev_foreclosed(Double.valueOf(jsonObject.get("PrevForeclosed").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("ForeclosureRatio"))) {
                marketHealthResult.setForeclosure_ratio(Double.valueOf(jsonObject.get("ForeclosureRatio").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("ZHVI"))) {
                marketHealthResult.setZhvi(Double.valueOf(jsonObject.get("ZHVI").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("MoM"))) {
                marketHealthResult.setMom(Double.valueOf(jsonObject.get("MoM").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("YoY"))) {
                marketHealthResult.setYoy(Double.valueOf(jsonObject.get("YoY").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("ForecastYoYPctChange"))) {
                marketHealthResult.setForecast_yoy_pct_change(Double.valueOf(jsonObject.get("ForecastYoYPctChange").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("StockOfREOs"))) {
                marketHealthResult.setStock_of_reos(Double.valueOf(jsonObject.get("StockOfREOs").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("NegativeEquity"))) {
                marketHealthResult.setNegative_equity(Double.valueOf(jsonObject.get("NegativeEquity").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("Delinquency"))) {
                marketHealthResult.setDelinquency(Double.valueOf(jsonObject.get("Delinquency").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("DaysOnMarket"))) {
                marketHealthResult.setDays_on_market(Double.valueOf(jsonObject.get("DaysOnMarket").toString()));
            }

        }

        return marketHealthResult;
    }

    public static ZriResult extractZriResultByJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        ZriResult zriResult = new ZriResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);

            if (!StringUtils.isEmpty(jsonObject.get("Zri"))) {
                zriResult.setZri(Double.valueOf(jsonObject.get("Zri").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("MoM"))) {
                zriResult.setMom(Double.valueOf(jsonObject.get("MoM").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("QoQ"))) {
                zriResult.setQoq(Double.valueOf(jsonObject.get("QoQ").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("YoY"))) {
                zriResult.setYoy(Double.valueOf(jsonObject.get("YoY").toString()));
            }
            if (!StringUtils.isEmpty(jsonObject.get("ZriRecordCnt"))) {
                zriResult.setZri_record_cnt(Double.valueOf(jsonObject.get("ZriRecordCnt").toString()));
            }
        }
        return zriResult;
    }

    public static BreakEvenPRResult extractBreakEvenPRResultByJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        BreakEvenPRResult breakEvenPRResult = new BreakEvenPRResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            //BEPropCount	SampleRate	MedBE	Breakeven	MedPR
            if (!StringUtils.isEmpty(jsonObject.get("BEPropCount"))) {
                breakEvenPRResult.setBe_prop_count(Long.valueOf(jsonObject.getString("BEPropCount")));
            }
            if (!StringUtils.isEmpty(jsonObject.get("SampleRate"))) {
                breakEvenPRResult.setSample_rate(Double.valueOf(jsonObject.getString("SampleRate")));
            }
            if (!StringUtils.isEmpty(jsonObject.get("MedBE"))) {
                breakEvenPRResult.setMed_be(Double.valueOf(jsonObject.getString("MedBE")));
            }
            if (!StringUtils.isEmpty(jsonObject.get("Breakeven"))) {
                breakEvenPRResult.setBrea_keven(jsonObject.getString("Breakeven"));
            }
            if (!StringUtils.isEmpty(jsonObject.get("MedPR"))) {
                breakEvenPRResult.setMed_pr(Double.valueOf(jsonObject.get("MedPR").toString()));
            }
        }
        return breakEvenPRResult;
    }

    public static ZriForecastPublicResult extractZriForecastPublicResultByJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        ZriForecastPublicResult zriForecastPublicResult = new ZriForecastPublicResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (!StringUtils.isEmpty(jsonObject.get("ZriForecast"))) {
                zriForecastPublicResult.setZri_forecast(Long.valueOf(jsonObject.getString("ZriForecast")));
            }
            if (!StringUtils.isEmpty(jsonObject.get("Forecast_YoY_Change"))) {
                zriForecastPublicResult.setForecast_yoy_change(Double.valueOf(jsonObject.getString("Forecast_YoY_Change")));
            }
            if (!StringUtils.isEmpty(jsonObject.get("Current_YoY_Change"))) {
                zriForecastPublicResult.setCurrent_yoy_change(Double.valueOf(jsonObject.getString("Current_YoY_Change")));
            }
        }
        return zriForecastPublicResult;
    }

    public static List<TimeValueItemResult> getTimeSeriesDataByYear(String json, Integer year) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        List<TimeValueItemResult> timeValueItemResult = null;
        try {
            timeValueItemResult = mapper.readValue(json, new TypeReference<List<TimeValueItemResult>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        //0年 表示获取所有数据
        if (year != 0 && year > 0) {
            String dateFormat = "yyyy-MM-dd";
            Date eTime = new Date();
            Date bTime = DateUtils.addYear(eTime, -year);
            //开始截取的时间
            String startTime = DateUtils.formatDate(bTime, dateFormat);
            Boolean flag = false; // 开始截取标识
            List<TimeValueItemResult> items = new ArrayList<>();
            for (TimeValueItemResult item : timeValueItemResult) {
                String time = item.getTime();
                if (time.indexOf(" ") > -1) {
                    time = time.substring(0, time.indexOf(" "));
                }
                Integer a = Integer.valueOf(time.replaceAll("-", ""));
                Integer b = Integer.valueOf(startTime.replaceAll("-", ""));
                if (a >= b) {
                    flag = true;
                }
                if (flag) {
                    items.add(item);
                }
            }
            return items;
        }
        return timeValueItemResult;
    }

    public static List<TimeValueItemResult> getSeriesDataByYear(String json, Integer year) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        if (jsonObject.get("time") != null) {
            String time = jsonObject.get("time").toString();
            ObjectMapper mapper = new ObjectMapper();
            List<TimeValueItemResult> timeValueItemResult = null;
            try {
                timeValueItemResult = mapper.readValue(time, new TypeReference<List<TimeValueItemResult>>() {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            //0年 表示获取所有数据
            if (year != 0 && year > 0) {
                String dateFormat = "yyyy-MM";
                Date eTime = new Date();
                Date bTime = DateUtils.addYear(eTime, -year);
                //开始截取的时间
                String startTime = DateUtils.formatDate(bTime, dateFormat);
                Boolean flag = false; // 开始截取标识
                List<TimeValueItemResult> items = new ArrayList<>();
                for (TimeValueItemResult item : timeValueItemResult) {
                    Integer a = Integer.valueOf(item.getTime().replaceAll("-", ""));
                    Integer b = Integer.valueOf(startTime.replaceAll("-", ""));
                    if (a >= b) {
                        flag = true;
                    }
                    if (flag) {
                        items.add(item);
                    }
                }
                return items;
            }
            return timeValueItemResult;
        }
        return null;
    }

    public static List<TimeSeriesItem> getSubString(List<TimeSeriesItem> list, String time, String format) {
        if (list == null || list.size() < 1) {
            return null;
        }
        Date etime = new Date();
        Date btime = DateUtils.addYear(etime, -Integer.parseInt(time));
        //开始截取的时间
        String startTime = DateUtils.formatDate(btime, format);
        Boolean flag = false; // 开始截取标识
        List<TimeSeriesItem> items = new ArrayList<>();
        for (TimeSeriesItem item : list) {
            if (item.getTime().compareTo(startTime) > -1) {
                flag = true;  //从这个开始截取
            }
            if (flag) {
                items.add(item);
            }
        }
        return items;
    }

    public static List<TimeValueItemResult> dislodgeEmptyItem(List<TimeValueItemResult> items) {
        if (items == null || items.size() < 1) {
            return null;
        }
        List<TimeValueItemResult> list = new ArrayList<>();
        for (TimeValueItemResult item : items) {
            if (!StringUtils.isEmpty(item.getValue())) {
                list.add(item);
            }
        }
        return list;
    }
}
